package com.nami.store.biz.grphismsgs.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import com.nami.store.api.request.message.GrpMsgRequest;
import com.nami.store.api.request.message.HisMsgQryRequest;
import com.nami.store.api.response.message.MessageDTO;
import com.nami.store.biz.grphismsgs.convert.GrpMsgsConvert;
import com.nami.store.biz.grphismsgs.repository.GrpHisMsgsRepository;
import com.nami.store.biz.grphismsgs.service.GrpMsgDomainService;
import com.nami.store.biz.inboxmsgs.repository.InboxMsgsRepository;
import com.nami.store.biz.sendboxmsgs.repository.SendboxMsgsRepository;
import com.nami.store.domain.GrpHisMsgsDO;
import com.nami.store.domain.InboxMsgsDO;
import com.nami.store.domain.SendboxMsgsDO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class GrpMsgDomainServiceImpl implements GrpMsgDomainService {

    private final SendboxMsgsRepository sendboxMsgsRepository;

    private final InboxMsgsRepository inboxMsgsRepository;

    private final GrpHisMsgsRepository grpHisMsgsRepository;

    @Override
    public boolean storeGrpMsg(GrpMsgRequest request) {

        TimeInterval timer = DateUtil.timer();

        // 发件箱
        SendboxMsgsDO sendboxMsgs = new SendboxMsgsDO();
        sendboxMsgs.setUserId(request.getSenderId());
        sendboxMsgs.setMsgId(request.getMessageId());
        sendboxMsgs.setTargetId(request.getCid());
        sendboxMsgs.setAppKey(request.getAppKey());
        sendboxMsgs.setPayload(request.getPayload());
        sendboxMsgs.setPayloadType(request.getPayloadType());
        sendboxMsgs.setSendTime(request.getSendTime());
        sendboxMsgs.setConverType(2); // c2g
        sendboxMsgsRepository.save(sendboxMsgs);

        // 收件箱
        if (request.getReceiverIds() != null) {
            List<InboxMsgsDO> inboxMsgsList = new ArrayList<>(request.getReceiverIds().size());
            InboxMsgsDO inboxItem;
            for (String receiverId : request.getReceiverIds()) {
                inboxItem = new InboxMsgsDO();
                inboxItem.setMsgId(request.getMessageId());
                inboxItem.setUserId(receiverId);
                inboxItem.setTargetId(request.getCid());
                inboxItem.setAppKey(request.getAppKey());
                inboxItem.setPayload(request.getPayload());
                inboxItem.setPayloadType(request.getPayloadType());
                inboxItem.setSendTime(request.getSendTime());
                inboxItem.setConverType(2); // c2g
                inboxMsgsList.add(inboxItem);
            }
            // TODO 删除，
            // 统一在消息分发时候，进行添加。不在此处处理
            inboxMsgsRepository.batchSave(inboxMsgsList);
        }

        // 历史消息
        GrpHisMsgsDO grpHisMsgs = new GrpHisMsgsDO();
        grpHisMsgs.setMsgId(request.getMessageId());
        grpHisMsgs.setAppKey(request.getAppKey());

        grpHisMsgs.setSenderId(request.getSenderId());
        grpHisMsgs.setConverId(request.getCid());
        grpHisMsgs.setReceiverId(request.getCid());


        grpHisMsgs.setPayload(request.getPayload());
        grpHisMsgs.setPayloadType(request.getPayloadType());
        grpHisMsgs.setSendTime(request.getSendTime());
        grpHisMsgs.setConverType(2); // c2g

        grpHisMsgs.setSeq(request.getSeq());

        grpHisMsgs.setMemberCount(0);
        if (request.getReceiverIds() != null) {
            grpHisMsgs.setMemberCount(request.getReceiverIds().size());
        }
        grpHisMsgs.setReadCount(0);

        grpHisMsgs.setDelFlag("0");

        grpHisMsgsRepository.save(grpHisMsgs);
        if (timer.interval() > 500) {
            log.warn("[grp-save] messageId={} user:{}ms", request.getMessageId(), timer.interval());
        }

        return Boolean.TRUE;
    }

    @Override
    public List<MessageDTO> getGrpHisMsgs(HisMsgQryRequest request) {
        List<GrpHisMsgsDO> grpHisMsgs = grpHisMsgsRepository.getGrpHisMsgs(request);
        if (CollUtil.isEmpty(grpHisMsgs)) {
            return null;
        }
        return GrpMsgsConvert.INSTANCE.convertList(grpHisMsgs);

    }
}
